//
//  MycommentTabView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/15.
//  Copyright © 2016年 game. All rights reserved.
//

#import "MycommentTabView.h"
#import "MyCommentOfIslanCell.h"
#import "MyCommentModel.h"
#import "MycommetListModel.h"
#import "SpacePagerModel.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface MycommentTabView ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *dataAry;
@property (nonatomic,assign) CGFloat CellHeight;
@property (nonatomic,assign) NSInteger nextpage;
@property (nonatomic,strong) MyCommentModel *model;
@end

@implementation MycommentTabView

-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{

    if (self = [super initWithFrame:frame style:style]) {
        
        self.dataSource = self;
        self.delegate = self;
        [self registerNib:[UINib nibWithNibName:@"MyCommentOfIslanCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        self.separatorStyle = 0;
        
        self.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(RefreshHead)];
        
        self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(RefreshFooter)];

        __weak typeof(self) weakSelf = self;
        self.TapBlok = ^{
            
            [weakSelf RefreshHead];
        };

        self.nextpage = 2;
    }
    return self;
}

-(void)setUrl:(NSString *)url{

    _url = url;
    [self getDataWhithPage:1];

}

-(void)getDataWhithPage:(NSInteger )page{
    
    [[Manager sharedManager]RequestWhithURL:self.url AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
       
        if (error) {
            self.isShowFile = YES;
            [self reloadData];
        }else{
            self.model = [[MyCommentModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
            
            [self.dataAry addObjectsFromArray:_model.comment_list];
            [self.header endRefreshing];
            [self.footer endRefreshing];
            
            self.isShowImage = YES;
            [self reloadData];
        }
    }];
    
}

-(void)RefreshFooter{
    
    SpacePagerModel *pageModel  = self.model.pager;
    if (self.nextpage >pageModel.total_page.integerValue) {
        [self.footer noticeNoMoreData];
    }else
        [self getDataWhithPage:self.nextpage];
    self.nextpage ++;
    
}
-(void)RefreshHead{
    
    [self.dataAry removeAllObjects];
    [self getDataWhithPage:1];
    self.nextpage = 2;
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.CellHeight;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCommentOfIslanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.dataAry[indexPath.row];
    self.CellHeight = cell.cellHight;
    return cell;
}

@end